<?php
class Module extends My_Model {
	var	$_table			= 'modules';

    function __construct()
    {
        parent::My_Model();
    }

	function listModule($parent_id=null){
		static $res = NULL;
		if ($res === NULL)
		{
			$dbPrefix = $this->db->dbprefix;
			$table = $this->db->dbprefix.$this->_table;
			$this->db->dbprefix = '';
			$this->db->select("$table.*,COUNT(s.id) AS has_children");
			$this->db->from($table);
			$this->db->join("$table AS s", "s.parent_id={$table}.id", 'left');
			if($parent_id!=null) $this->db->where(array("{$table}.parent_id"=>$parent_id));
			$this->db->group_by("$table.id");
			$this->db->order_by('parent_id,id,sort_order ASC');
			$this->db->dbprefix = $dbPrefix;
			//$this->db->cache_on();
			$query = $this->db->get();
			//$this->db->cache_off();
			foreach ($query->result_array() as $row)
			{
			  $res[] = $row;
			}
		}
		return $res;
	}


	function listAsLevel($param = array()){
		//����ʹ��$param���鸲�����±���
		$parent_id=$selected=$level=0;
		$is_show_all = $re_type = false;
		$res = $this->listModule($parent_id);
		$this->load->helper('system');
		$options = cat_options($parent_id, $res);
		return cat_list($options,$parent_id,$selected,$re_type,$level,$is_show_all);
		return $rs;
	}
}